package com.qf.face.client.utils;


import javax.sound.sampled.*;
import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * @author 30909
 */
public class SoundUtil {

    /**
     * 音频文件
     */
    private String musicPath;
    /**
     * 音频播放标志
     */
    private volatile boolean run = false;
    /**
     * 播放音频的任务线程
     */
    private Thread mainThread;

    /**
     * 音频的流
     */
    private AudioInputStream audioStream;

    /**
     * 音频的格式
     */
    private AudioFormat audioFormat;

    /**
     * 音频的数据源
     */
    private SourceDataLine sourceDataLine;

    public  SoundUtil(String musicPath) {
        this.musicPath = musicPath;
        prefetch();
    }

    /**
     * 数据准备
     */
    private void prefetch() {

        try {
            //获取音频的输入流
            File audio = new File(musicPath);
            audioStream = AudioSystem.getAudioInputStream(audio);
            audioFormat = audioStream.getFormat();
            //包装音频信息
            DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,
                    audioFormat,
                    AudioSystem.NOT_SPECIFIED);

            sourceDataLine  = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
            sourceDataLine.open(audioFormat);
            sourceDataLine.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 关闭数据源
     */
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        sourceDataLine.drain();
        sourceDataLine.close();
        audioStream.close();
    }

    /**
     * 播放
     */
    private void playMusic(boolean loop) {
        try {
            if (loop) {
                while (true) {
                    playMusic();
                }
            } else {
                playMusic();
                sourceDataLine.drain();
                sourceDataLine.close();
                audioStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void playMusic() {
        try {
            synchronized (this) {
                run = true;
            }

            audioStream = AudioSystem.getAudioInputStream(new File(musicPath));
            int count ;

            byte tempBuff[] = new byte[1024];

            while ((count = audioStream.read(tempBuff, 0, tempBuff.length)) != -1) {
                synchronized (this) {
                    while (!run) {
                        wait();
                    }
                }
                sourceDataLine.write(tempBuff, 0, count);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void stopMusic(){
        synchronized (this){
            run  = false;
            //唤醒所有线程
            notifyAll();
        }
    }

    private void continueMusic(){
        synchronized (this){
            run  = true;
            //唤醒所有线程
            notifyAll();
        }
    }


    public void start(boolean loop) {
        mainThread = new Thread(new Runnable() {
            @Override
            public void run() {
                playMusic(loop);
            }
        });
        mainThread.start();
    }

    /**
     * 成功
     */
    public static void success() {
        SoundUtil success = new SoundUtil("E:\\Study\\automation\\facd-client\\src\\main\\resources\\audio\\success.WAV");
        success.start(false);
    }


    /**
     * 失败
     */
    public static void error() {
        SoundUtil error = new SoundUtil("E:\\Study\\automation\\facd-client\\src\\main\\resources\\audio\\error.WAV");
        error.start(false);

    }

    public static void main(String[] args) throws Exception{
        success();
        TimeUnit.SECONDS.sleep(10);
        error();
    }
}
